/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package biofilter.managers;


import biofilter.pipelines.*;
import biofilter.records.Record;
import biofilter.sources.*;
import biofilter.util.SystemProperties;
import biofilter.util.ArrayUtil;
import java.io.IOException;
import java.util.Collection;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.cli.*;
/**
 * Transcript mapper is an application for mapping transcript sequencing or
 * similar data back to genomes.  The transcript mapper is independent of the
 * mapper used (infact, we may use multiple mappers.
 *
 * @author Daniel Quest
 * @date   Jan 5 2010
 */
public class transcript_mapper {

    BowtieHistoPipeline bhp = null;
    BowtieMapperPipeline bmp = null;

    String bowtie = null;
    String tempdir = null;
    public void run(){
        try {
            ArrayUtil aute = new ArrayUtil();
            SystemProperties sprop = new SystemProperties();
            bowtie = sprop.get("bowtie");
            tempdir = sprop.get("tmp");
            Logger.getLogger(transcript_mapper.class.getName()).log(Level.INFO, "using bowtie : " + bowtie);
            Logger.getLogger(transcript_mapper.class.getName()).log(Level.INFO, "using tmp : " + tempdir);

            //String trial = "/auto/transcriptomics/data/runfiles/test.run";
            //String db = "/auto/transcriptomics/data/runfiles/Burkholderia_cenocepacia_HI2424.db";
            String trial = "/auto/transcriptomics/data/runfiles/Clostridium_thermocellum_ATCC_27405.run";
            String db = "/auto/transcriptomics/data/runfiles/Clostridium_thermocellum_ATCC_27405.db";
            RecordsFile fnrf = new RecordsFile(trial, "biofilter.records.FileNameRecord");
            RecordsFile dbrf = new RecordsFile(db, "biofilter.records.FileNameRecord");
            //System.err.println(fnrf.getRecord() );

            //while (!dbrf.eof()){
                //run the mapper on a series of input files on one database
                bmp = new BowtieMapperPipeline(trial);
                bmp.setDatabaseFile(dbrf.getRecord().get());

                //run the histogram creator for all files generated by the mapper
                Vector<Record> v = bmp.execPipeline();
                Record [] r = aute.toArray(v);
                aute.printArray(r);
                RecordsFile bowtieRecords = new RecordsFile(r, "biofilter.records.FileNameRecord");
                //bhp = new BowtieHistoPipeline(bowtieRecords);
                //bhp.execPipeline();
            //}
        } catch (IOException ex) {
            Logger.getLogger(transcript_mapper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(transcript_mapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


        /**
     * This is a generic setup function.  Specific pipelines may need to implement similar logic for specific options they wish to implement
     */
    private Options options = null;
    public void setup(String [] args) throws org.apache.commons.cli.ParseException{
            options = new Options();

            //Setup Options
            //Boolean Options
            Option help = new Option( "h", "help", false, "print help message" );
            Option unique = new Option("u", "unique", false, "only map reads if they align to one position");

            //Param Options
            Option infile   = new Option("i", "infile", true, "concrete source file e.g. a line delimited file containing fasta filenames" );
            Option database = new Option("d", "database", true, "concrete source file e.g. a line delimited file containing database names");
            Option outfile   = new Option("o", "outfile", true, "concrete sink file e.g. a line delimited file containing result filenames" );
            Option mapped = new Option("m", "mapped", true, "temporary file to place all mapped reads from the run");
            Option unmapped = new Option("n", "unmapped", true, "temporary file to place all unmapped reads from the run");

            //Add command line options to influence behavior
            options.addOption( help );
            options.addOption(infile);
            options.addOption(database);
            options.addOption(outfile);

            CommandLineParser parser = new PosixParser();
            CommandLine cmd = parser.parse( options, args);

            //Logic to handle standard command line options such as help

            if (cmd.hasOption("help") == true || cmd.hasOption("infile") == false || cmd.hasOption("outfile") == false){
                // automatically generate the help statement
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp( "SeqGenOpts", options );
                System.exit(0);
            }

    }


    public static void main(String[] args){
        transcript_mapper tm = new transcript_mapper();
        tm.run();
    }
}
